// const app = require('express')()

//导入数据库操作模块
const db = require("../../db/index")
//导入uuid模块
const uuid = require('node-uuid')

const moment = require('moment')

/**
 * 根据oppenid获取历史记录
 * @param {*} req 
 * @param {*} res 
 */
exports.getVideoHistory = (req, res) => {
    // console.log('获取学习视频列表'+req.ip)
    const uid = req.query.uid
    // console.log('title' + title)
    const sql = `SELECT videos.id,videos.title ,videos.video_url,videos.img_url,videos.flag,videos.publisher,history.date FROM xks_study_videos AS videos, xks_video_history AS history WHERE videos.id = history.v_id AND history.u_id = ? ORDER BY history.date DESC`
    db.query(sql, uid, (err, results) => {
        if (err) {
            return res.send({
                code: 505,
                msg: '发送了错误' + err.message,
                data: {}
            })
        }

        if (!results.length > 0) {
            return res.send({
                code: 400,
                msg: '抱歉，没有找到你想要的内容',
                data: {}
            })
        }

        return res.send({
            code: 200,
            msg: '成功',
            data: {
                list: results
            }
        })
    })
}


exports.addVideoHistory = (req, res) => {
    const videoHistory = req.body;
    if (JSON.stringify(videoHistory) == "{}") {
        console.log('【增加申请发送错误】' + 'req.body ===> ', req.body)
        return res.send({
            code: 400,
            msg: '因发送的请求语法错误,服务器无法正常读取。',
            data: {}
        })
    }
    else {
        if (!videoHistory.u_id || !videoHistory.v_id) {
            return res.send({
                code: 400,
                msg: '有必填字段未填，请重新操作。',
                data: {}
            })
        }
    }



    //浏览时间
    let nowTime = new Date()
    const TODAY = moment(nowTime).format('YYYY-MM-DD')
    var formatTime = moment(nowTime).format('YYYY-MM-DD HH:mm')
    // console.log('浏览时间', formatTime)
    videoHistory['date'] = formatTime;

    //该当天的记录是否已存在，若存在则更新
    const findSql = `SELECT * FROM xks_video_history WHERE u_id = ? AND v_id = ? AND date between '${TODAY} 00:00' and '${TODAY} 24:59'`
    db.query(findSql, [videoHistory.u_id, videoHistory.v_id], (err, results) => {
        if (err) {
            console.log(err.message)
            return res.send({
                code: 505,
                msg: err.message,
                data: {}
            })
        }
        if (results.length > 0) {
            //进行时间的更新 
            const updateSql = `UPDATE xks_video_history SET date = ? WHERE id = ? `
            db.query(updateSql, [formatTime, results[0].id], (err, result) => {
                if (err) {
                    console.log(err.message)
                    return res.send({
                        code: 505,
                        msg: err.message,
                        data: {}
                    })
                }
                if (result.affectedRows !== 1) {
                    return res.send({
                        code: 500,
                        msg: '更新历史记录失败，请稍后再试！',
                        data: {}
                    })
                }
                res.send({
                    code: 200,
                    msg: '更新历史记录成功',
                    data: {}
                })
            })
        }
        else {

            //生成uuid
            // let uuid1 = uuid.v1();
            // // 去除横线-
            // uuid1 = uuid1.replace(/-/g, '');
            // // 添加id
            // videoHistory['id'] = uuid1;

            const addSql = `INSERT INTO xks_video_history SET ? `
            db.query(addSql, videoHistory, (err, result) => {
                if (err) {
                    console.log(err.message)
                    return res.send({
                        code: 505,
                        msg: err.message,
                        data: {}
                    })
                }
                if (result.affectedRows !== 1) {
                    return res.send({
                        code: 500,
                        msg: '增加失败，请稍后再试！',
                        data: {}
                    })
                }
                res.send({
                    code: 200,
                    msg: '增加成功',
                    data: {}
                })
            })
        }
    })
}
